'use strict';

angular.module('xianwan').factory('MessagePacket', function () {

    return {

        pack: function (type, data) {
            let v1 = new Int8Array([type << 4]);
            let v2 = new Int8Array(1);
            let v3 = new Int16Array(1);

            if (data) {
                let len = encodeURI(data).split(/%..|./).length - 1;
                v3[0] = (len & 0xFF) << 8 | len >> 8;
            }

            let array = new Array(v1, v2, v3);
            if (data) {
                array.push(data);
            }

            return new Blob(array, {
                type: 'application/octet-binary'
            });
        }
    }
});